package de.schoar.nagroid.nagios;



import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import de.schoar.nagroid.DM;


public class NagiosHost {
	private NagiosSite mSite;
	private String mName;
	private NagiosState mState;
	private boolean mAck;
	
	private LinkedList<NagiosService> mServices = new LinkedList<NagiosService>();

	public NagiosHost(NagiosSite site, String hostname, NagiosState state) {
		mSite = site;
		mName = hostname;
		mState = state;
		mSite.addHost(this);
	}
	
	public NagiosHost(String hostname, NagiosState state) {
		mName = hostname;
		mState = state;
		mAck = false;
	}

	public void addChild(NagiosService ns) {
		mServices.add(ns);
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Host: " + mName + " State: " + mState + "\n");
		for (NagiosService ns : mServices) {
			sb.append(ns.toString() + "\n");
		}
		return sb.toString();
	}

	public String getName() {
		return mName;
	}

	public NagiosState getState() {
		return mState;
	}

	public void setState(NagiosState ns) {
		mState = ns;
	}
	
	public boolean getAck(){
		return mAck;
	}
	
	public void setAck(boolean ack){
		mAck = ack;
	}

	public LinkedList<NagiosService> getServices() {
		if(DM.I.getConfiguration().getSortType()==1) Collections.sort(mServices, new NagiosServiceComparator());
		return mServices;
	}
	
	public boolean found_service (String s){
		Iterator<NagiosService> it = mServices.iterator();
		while(it.hasNext()){
			String chaine = ((NagiosService)it.next()).getName();
			if(chaine.equals(s)){
				return true;
			}
		}
		return false;
	}
	
	
	
	public void findPing(){
		NagiosService ping=null;
		NagiosService ns;
		boolean end = false;
		Iterator it = mServices.iterator();
		while(!end && it.hasNext()){
			ns = (NagiosService)it.next();
			if(ns.getName().equalsIgnoreCase("Ping")){
				end = true;
				ping = ns; 
			}
		}
		mServices.clear();
		mServices.add(ping);
	}
	
	
}

